package com.kaifamiao.jspcore;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/sign/in")
public class SignInServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String uname = request.getParameter( "username" );
        String passwd = request.getParameter( "password" );

        if( "aguang".equals( uname ) && "hello".equals( passwd ) ) {
            Loong x = new Loong( 2002, "敖广", '男' );
            // 将Loong实例作为取值设置到请求对象的属性中
            request.setAttribute( "loong", x );
            // 将变量uname的值作为取值设置到请求对象的属性中
            request.setAttribute( "username", uname );

            RequestDispatcher rd = request.getRequestDispatcher( "/expression/status.jsp" );
            // 转发
            rd.forward(request, response);
            return;
        }

        HttpSession session = request.getSession();
        session.setAttribute( "message", "用户名或密码错误" );
        // 重定向
        response.sendRedirect( "/index.jsp" );
    }

}
